﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cspl.AnimalFight.Lib;
using Cspl.AnimalFight.Lib.Data;

namespace Cspl.AnimalFight.Main
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BattleField battleField = new BattleField();
            string player1 = "";
            string player2 = "";
            int turn = 1;

            Dog dog = new Dog
            {

                Name = "Doggy",
                HP = 1000,
                DP = 100,
                AP = 300,
                Mood = DogMood.Good
            };

            Dog dog2 = new Dog
            {

                Name = "Bernard",
                HP = 1000,
                DP = 150,
                AP = 250,
                Mood = DogMood.Bad
            };

            Duck duck = new Duck
            {
                Name = "Donal",
                HP = 1000,
                DP = 200,
                AP = 500,
            };

            Snake snake = new Snake
            {
                Name = "Snake",
                HP = 1000,
                DP = 50,
                AP = 300,
            };

            Horse horse = new Horse
            {
                Name = "Makibao",
                HP = 1000,
                DP = 200,
                AP = 200,
            };

            Console.WriteLine("/--------------------------------------");
            Console.WriteLine("/");
            Console.WriteLine("/   Welcome to Animal Battle Game ;)   ");
            Console.WriteLine("/");
            Console.WriteLine("/--------------------------------------");
            Console.WriteLine("/");
            Console.WriteLine("/ Please, select an animal");
            Console.WriteLine("/ There are Doggy, Bernard, Donal, Snake and Makibao");
            Console.WriteLine("/ Type the animal choosen>");
           
            bool notValid = true;
            string player1Type = "";
            while (notValid)
            {
                Console.Write("/ Player 1: ");
                player1 = Console.ReadLine();
                player1 = player1.ToLower();
                player1Type = player1;
                switch (player1)
                {
                    case "doggy":
                        battleField[dog.Name] = dog;
                        Console.WriteLine("/ Player 1 choose {0}", dog.Name);
                        player1 = dog.Name;
                        notValid = false;
                        break;
                    case "bernard":
                        battleField[dog2.Name] = dog2;
                        Console.WriteLine("/ Player 1 choose {0}", dog2.Name);
                        player1 = dog2.Name;
                        notValid = false;
                        break;
                    case "donal":
                        battleField[duck.Name] = duck;
                        Console.WriteLine("/ Player 1 choose {0}", duck.Name);
                        player1 = duck.Name;
                        notValid = false;
                        break;
                    case "makibao":
                        battleField[horse.Name] = horse;
                        Console.WriteLine("/ Player 1 choose {0}", horse.Name);
                        player1 = horse.Name;
                        notValid = false;
                        break;
                    case "snake":
                        battleField[snake.Name] = snake;
                        Console.WriteLine("/ Player 1 choose {0}", snake.Name);
                        player1 = snake.Name;
                        notValid = false;
                        break;
                    default:
                        Console.WriteLine("Oooops. wrong input...");
                        Console.WriteLine("Please type the right input...");
                        Console.WriteLine("dog, duck, horse, or snake");
                        break;
                }
            }

            notValid = true;
            while (notValid)
            {
                Console.Write("/ Now is Player 2: ");
                player2 = Console.ReadLine();
                player2 = player2.ToLower();
                if (!player1Type.Equals(player2))
                {
                    switch (player2)
                    {
                        case "doggy":
                            battleField[dog.Name] = dog;
                            Console.WriteLine("/ Player 2 choose {0}", dog.Name);
                            player2 = dog.Name;
                            notValid = false;
                            break;
                        case "bernard":
                            battleField[dog2.Name] = dog2;
                            Console.WriteLine("/ Player 2 choose {0}", dog2.Name);
                            player2 = dog2.Name;
                            notValid = false;
                            break;
                        case "donal":
                            battleField[duck.Name] = duck;
                            Console.WriteLine("/ Player 2 choose {0}", duck.Name);
                            player2 = duck.Name;
                            notValid = false;
                            break;
                        case "makibao":
                            battleField[horse.Name] = horse;
                            Console.WriteLine("/ Player 2 choose {0}", horse.Name);
                            player2 = horse.Name;
                            notValid = false;
                            break;
                        case "snake":
                            battleField[snake.Name] = snake;
                            Console.WriteLine("/ Player 2 choose {0}", snake.Name);
                            player2 = snake.Name;
                            notValid = false;
                            break;
                        default:
                            Console.WriteLine("Oooops. wrong input...");
                            Console.WriteLine("Please type the right input...");
                            Console.WriteLine("dog, duck, horse, or snake");
                            break;
                    }
                }
                else
                {
                    Console.WriteLine("{0} has been choosen by Player 1.", player1Type);
                    Console.WriteLine("Please select another animal.");
                }
            }

            Console.WriteLine();
            Console.WriteLine("/ Press any key to start battle");
            Console.ReadKey();
            Console.Clear();

            string commandLine;
            string command = string.Empty;
            bool valid = true;
            string player = "";
            string target = "";
            while (command != "exit")
            {
                Console.WriteLine("/--------------------------------------");
                Console.WriteLine("/");
                Console.WriteLine("/   Player {0}'s turn   ", turn);
                Console.WriteLine("/");
                Console.WriteLine("/ command: attack, defend, escape, exit");
                Console.WriteLine("/--------------------------------------");
                Console.WriteLine("/");
                Console.Write("/ Player {0}: ", turn);

                commandLine = System.Console.ReadLine();
                string[] commands = commandLine.Split(' ');
                command = commands[0].ToLower();
                
                
                if (valid)
                {
                    player = turn == 1 ? player1 : player2;
                    target = player == player1 ? player2 : player1;
                }
                
                
                switch (command)
                {
                    case "attack":
                        valid = true;
                        if (commands.Length == 1)
                        {
                            valid = battleField.Command(player, Commands.Attack, target); 
                        }
                        else
                        {
                            Console.WriteLine("Invalid command for attack.");
                        }
                        
                        break;
                    case "defend":
                        valid = true;
                        if (commands.Length == 1)
                        {
                            valid = battleField.Command(player, Commands.Defend, string.Empty); 
                        }
                        else
                        {
                            Console.WriteLine("Invalid command for defend.");
                        }
                        
                        break;
                    case "escape":
                        if (commands.Length == 1)
                        {
                            battleField.Command(player, Commands.Escape, string.Empty);
                        }
                        else
                        {
                            Console.WriteLine("Invalid command for escape."); 
                        }
                        valid = true;
                        break;
                    case "exit":
                        Console.WriteLine("Exit the application.");
                        break;
                    default:
                        Console.WriteLine("Invalid command.");
                        valid = false;
                        break;
                }
                if (valid)
                {
                    if (battleField[target].IsDead)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Congratulations.");
                        Console.WriteLine("Player {0} wins", turn);
                        command = "exit";
                    }
                    if (battleField[player].HasEscaped)
                    {
                        turn = turn == 2 ? --turn : ++turn;
                        Console.WriteLine();
                        Console.WriteLine("Congratulations.");
                        Console.WriteLine("Player {0} wins", turn );
                        command = "exit";
                    }
                    turn = turn == 2 ? --turn : ++turn;
                }
                
                Console.WriteLine();
                Console.WriteLine("/ Press any key to continue");
                Console.ReadKey();
                Console.Clear();
            }
        }
    }
}
